import Vue from 'vue';
import VueRouter from 'vue-router';

Vue.use(VueRouter);

function importRoutes() {
  const files = require.context('./modules', false, /\.js$/);
  const routes = [];
  files.keys().forEach(key => {
    routes.push(...files(key).default);
  });
  return routes;
}

const routes = [
  ...importRoutes(),
  {
    path: '*',
    redirect: '/'
  }
];

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
  scrollBehavior: to => {
    if (to.hash) {
      return {
        selector: to.hash,
        behavior: 'smooth'
      };
    }
    return { x: 0, y: 0 };
  }
});

export default router;
